package tree.test113;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 8:33
 */
public class Solution2 {

    // 解法二 ： 简单的DFS
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if (root == null) return res;
        dfs(root, sum, 0, new ArrayList<>());
        return res;
    }

    private void dfs(TreeNode root, int sum, int c, List<Integer> path) {
        path.add(root.val);
        c += root.val;
        if (root.left == null && root.right == null) {
            if (sum == c) res.add(new ArrayList<>(path));
            return;
        }
        if (root.left != null) dfs(root.left, sum, c, new ArrayList<>(path));
        if (root.right != null) dfs(root.right, sum, c, new ArrayList<>(path));
    }
}
